import $ from 'jquery'


//异步Get请求
export function HttpGet(url, param, callback) {
    // jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "GET",
        dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Access-Control-Allow-Origin", "*"); //设置跨域访问信息
            request.setRequestHeader("Token", _tokenID);
        },
        success: function (data) {
            callback(data)
        },
        error: function (data) {
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}
//异步post请求
export function HttpPost(url, param, callback) {
    // showFullScreenLoading();
    // $.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "POST",
        dataType: "json",
        contentType:"application/json;charset=utf-8",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Token", _tokenID);
            // request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        },
        success: function (data) {
            callback(data)
        },
        error: function (data) {
            // hideFullScreenLoading();
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}
//异步post请求,非json请求头
export function HttpPostLogin(url, param, callback) {
    // showFullScreenLoading();
    // $.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "POST",
        dataType: "json",
        // contentType:"application/json;charset=utf-8",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Token", _tokenID);
            request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        },
        success: function (data) {
            callback(data)
        },
        error: function (data) {
            // hideFullScreenLoading();
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}

export default {
    HttpGet,
    HttpPost,
    HttpPostLogin
}
